// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of DOM3XPath
//
// DOM3XPath is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// DOM3XPath is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with DOM3XPath. If not, see <http://www.gnu.org/licenses/>.
//	
//
//   $Id$
// XPathHelper.cpp : Implementation of CXPathHelper
#include "stdafx.h"
#include <DOM3XPath.h>
#include "XPathHelper.h"

#ifndef _DEBUG
	//debug includes stdio, we need it for swprintf.
	#include <stdio.h>
#endif

/////////////////////////////////////////////////////////////////////////////
// CXPathHelper


STDMETHODIMP CXPathHelper::getData(VARIANT V, long *retVal)
{
	HRESULT hr = S_OK;
	//clean up.
	*retVal = 0;
	switch(V.vt)
	{
		case VT_DISPATCH:
			{
				CComQIPtr<IXMLDOMNodeList> pDOMSelection(V.pdispVal);
				if (pDOMSelection)
					hr = m_pResult->parseSelection(pDOMSelection);
			}
			break;
		case VT_BSTR:
			m_pResult->m_sStringValue = V.bstrVal;

			if(m_pResult->m_uResultType==CXPathResult::EXPathResultType::ANY_TYPE)
			{
				m_pResult->m_uResultType=CXPathResult::EXPathResultType::STRING_TYPE;
			}
			break;
		case VT_R8:
			m_pResult->m_dNumberValue = V.dblVal;
			if(m_pResult->m_uResultType==CXPathResult::EXPathResultType::STRING_TYPE)
			{	
//				convert number to a string
				char sBuff[19]; // 2 extra for sign and point then 1 for null
				_gcvt(V.dblVal,16,sBuff);
				char * pcDecimalPoint = strchr(sBuff,'.');
				if(pcDecimalPoint && pcDecimalPoint[1] == '\0')
				{
					*pcDecimalPoint = '\0';
				}
				//swprintf(sBuff,L"%1.15f",V.dblVal);
				m_pResult->m_sStringValue = CComBSTR(sBuff);
				
			}
			else if(m_pResult->m_uResultType==CXPathResult::EXPathResultType::ANY_TYPE)
			{
				m_pResult->m_uResultType=CXPathResult::EXPathResultType::NUMBER_TYPE;
			}
			else if(CXPathResult::EXPathResultType::BOOLEAN_TYPE == this->m_pResult->m_uResultType)
			{
				m_pResult->m_bBooleanValue = (V.dblVal != 0);
			}
			break;
		case VT_BOOL:
			
			if(this->m_pResult->m_uResultType==CXPathResult::EXPathResultType::ANY_TYPE || this->m_pResult->m_uResultType==CXPathResult::EXPathResultType::BOOLEAN_TYPE)
			{
				m_pResult->m_bBooleanValue = V.boolVal;
				this->m_pResult->m_uResultType=CXPathResult::EXPathResultType::BOOLEAN_TYPE;
			}
			else if(this->m_pResult->m_uResultType==CXPathResult::EXPathResultType::STRING_TYPE)
			{
				m_pResult->m_sStringValue = CComBSTR(V.boolVal?"true":"false");
			}
			break;
		default:
			hr = S_FALSE;
	}
	
	return hr;
}

#ifdef COUNT_HELPER
	unsigned long CXPathHelper::sm_Count = 0;	
#endif
